﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common.Extension;
using Common.Model;
using Common.util;
using Dapper;
using log4net;
using MoreLinq;

namespace Common.Dao
{
    public class ClassifyModelDao
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(ClassifyModelDao));
        
        //获取关键词模型
        public static List<ClassifyModel> GetClassifyModel()
        {
            String sql =
                "SELECT word_prior_probability AS wordPriorProbability,word_conditiona_probability AS WordConditionaProbability,word,block,update_time AS UpdateTime FROM `classify_model`";

            using (var conn = DbUtils.GetHotTopicConnection())
            {
                return conn.Query<ClassifyModel>(sql)?.ToList();
            }
        }
        
                
        //获取关键词模型
        public static List<String> GetClassifyBlock()
        {
            String sql =
                "SELECT DISTINCT block FROM `classify_model`";

            using (var conn = DbUtils.GetHotTopicConnection())
            {
                return conn.Query<String>(sql)?.ToList();
            }
        }
        
        //获取关键词模型根据Block和Word
        public static List<ClassifyModel> GetClassifyModelByBlockWord(string block ,string wrod)
        {
            String sql =
                "SELECT word_prior_probability AS wordPriorProbability,word_conditiona_probability AS WordConditionaProbability,word,block,update_time AS UpdateTime FROM `classify_model` WHERE block = @Block and word = @Word";

            using (var conn = DbUtils.GetHotTopicConnection())
            {
                return conn.Query<ClassifyModel>(sql,new
                {
                    Word = wrod,
                    Block = block
                })?.ToList();
            }
        }
        
        //根据板块获取关键词模型
        public static List<ClassifyModel> GetClassifyModelByBlock(string block)
        {
            String sql =
                "SELECT word_prior_probability AS WordPriorProbability,word_conditiona_probability AS WordConditionaProbability,word,block,update_time AS UpdateTime FROM `classify_model` " +
                "WHERE block = " + block;
            using (var conn = DbUtils.GetHotTopicConnection())
            {
                return conn.Query<ClassifyModel>(sql)?.ToList();
            }
        }
        
        //保存关键词模型
        public static void BatchSaveMode(List<ClassifyModel> models)
        {
            int batchSize = 10000;
            var batchData = models.Batch(batchSize);
            foreach (var data in batchData)
            {
                var sql = new StringBuilder("REPLACE INTO `classify_model`(block,word,word_prior_probability,word_conditiona_probability,update_time) VALUES ");
                bool first = true;
                foreach (ClassifyModel model in data)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        sql.Append(",");
                    }

                    sql.Append($"('{model.Block}','{model.Word}','{model.WordPriorProbability}','{model.WordConditionaProbability}','{model.UpdateTime}')");
                }

                using (var cnn = DbUtils.GetHotTopicConnection())
                {
                    cnn.Execute(sql.ToString());
                    Log.Info("insert classify_model data");
                }
            }
        }
        
        public static void BatchDeleteMode(List<ClassifyModel> models)
        {
            String sql =
                @"DELETE FROM `classify_model` WHERE word = @Word and block = @Block";
            using (var conn = DbUtils.GetHotTopicConnection())
            {
                conn.InsertMultiple(sql, models);
                Log.Info("delete classify_model data");
            }
        }


        public static void ClearClassify()
        {
            String sql =
                @"DELETE FROM `classify_model` WHERE 1=1";
            using (var conn = DbUtils.GetHotTopicConnection())
            {
                conn.InsertMultiple(sql);
                Log.Info("clear classify_model data");
            }
        }
    }
}